package com.stationbuilding.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.stationbuilding.R;
import com.stationbuilding.bean.SelectFileBean;
import com.stationbuilding.constants.Constants;

import java.util.List;

public class ShowPhotoAdapter extends RecyclerView.Adapter<ShowPhotoAdapter.MyHolder> implements View.OnClickListener, View.OnLongClickListener {
    private Context context;
    private List<SelectFileBean> bitmapList;

    public ShowPhotoAdapter(Context context, List<SelectFileBean> bitmapList) {
        this.context = context;
        this.bitmapList = bitmapList;
    }

    public void setBitmapList(List<SelectFileBean> bitmapList) {
        this.bitmapList = bitmapList;
    }

    @Override
    public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.show_photo_item, parent, false);
        MyHolder holder = new MyHolder(view);
        view.setOnLongClickListener(this);
        view.setOnClickListener(this);
        return holder;
    }

    @Override
    public void onBindViewHolder(MyHolder holder, int position, List<Object> payloads) {
        if (payloads.isEmpty()) {
            ViewGroup.LayoutParams lp = holder.ivLoadBackground.getLayoutParams();
            lp.height = context.getResources().getDimensionPixelSize(R.dimen.pic_height);
            holder.ivLoadBackground.setLayoutParams(lp);
            onBindViewHolder(holder, position);
        } else {
            ViewGroup.LayoutParams lp = holder.ivLoadBackground.getLayoutParams();
            if (bitmapList.get(position).getPercent() > 1) {
                lp.height = context.getResources().getDimensionPixelSize(R.dimen.pic_height) * (100 - (int)(bitmapList.get(position).getPercent())) / 100;
            }
            holder.ivLoadBackground.setLayoutParams(lp);
            if (bitmapList.get(position).isLoaded()) {
                holder.ivLoadBackground.setVisibility(View.GONE);
                holder.ivLoad.setVisibility(View.GONE);
                holder.ivVideo.setVisibility(View.VISIBLE);
                holder.ivLoad.clearAnimation();
            }
        }
    }

    @Override
    public void onBindViewHolder(MyHolder holder, int position) {
        holder.itemView.setTag(position);
        if (bitmapList.get(position).getType() == Constants.PHOTO) {
            Glide.with(context).load(bitmapList.get(position).getPath()).into(holder.ivShowPhoto);
            holder.ivVideo.setVisibility(View.GONE);
            holder.ivLoad.setVisibility(View.GONE);
            holder.ivLoadBackground.setVisibility(View.GONE);
        } else {
            if (bitmapList.get(position).getPath().startsWith(Constants.BASE_URL)) {
                String[] paths = bitmapList.get(position).getPath().split(",");
                Glide.with(context).load(paths[0]).into(holder.ivShowPhoto);
                holder.ivLoad.setVisibility(View.GONE);
                holder.ivLoadBackground.setVisibility(View.GONE);
                holder.ivVideo.setVisibility(View.VISIBLE);
            } else {
                Glide.with(context).load(bitmapList.get(position).getPath()).into(holder.ivShowPhoto);
                if (bitmapList.get(position).isLoaded()) {
                    holder.ivLoad.setVisibility(View.GONE);
                    holder.ivLoadBackground.setVisibility(View.GONE);
                    holder.ivVideo.setVisibility(View.VISIBLE);
                } else {
                    Animation circle_anim = AnimationUtils.loadAnimation(context, R.anim.anim_round_rotate);
                    LinearInterpolator interpolator = new LinearInterpolator();  //设置匀速旋转，在xml文件中设置会出现卡顿
                    circle_anim.setInterpolator(interpolator);
                    holder.ivLoad.startAnimation(circle_anim);  //开始动画
                    holder.ivLoadBackground.setVisibility(View.VISIBLE);
                    holder.ivLoad.setVisibility(View.VISIBLE);
                    holder.ivVideo.setVisibility(View.GONE);
                }
            }
        }
    }

    @Override
    public int getItemCount() {
        return bitmapList.size();
    }

    @Override
    public boolean onLongClick(View v) {
        mOnItemLongClickListener.onItemLongClickListener(v, (Integer) v.getTag());
        return true;
    }

    @Override
    public void onClick(View v) {
        mOnItemClickListener.onItemClickListener(v, (Integer) v.getTag());
    }

    class MyHolder extends RecyclerView.ViewHolder {
        private ImageView ivShowPhoto;
        private ImageView ivVideo;
        private ImageView ivLoad;
        private ImageView ivLoadBackground;

        MyHolder(View itemView) {
            super(itemView);
            ivShowPhoto = (ImageView) itemView.findViewById(R.id.iv_show_photo);
            ivVideo = (ImageView) itemView.findViewById(R.id.iv_video);
            ivLoad = (ImageView) itemView.findViewById(R.id.iv_load);
            ivLoadBackground = (ImageView) itemView.findViewById(R.id.iv_load_background);
        }
    }

    private OnItemLongClickListener mOnItemLongClickListener;

    public void setOnLongItemClickListener(OnItemLongClickListener mOnItemLongClickListener) {
        this.mOnItemLongClickListener = mOnItemLongClickListener;
    }

    public interface OnItemLongClickListener {
        void onItemLongClickListener(View view, int position);
    }

    private OnItemClickListener mOnItemClickListener;

    public void setOnItemClickListener(OnItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }

    public interface OnItemClickListener {
        void onItemClickListener(View view, int position);
    }
}
